#!/bin/sh
#desc:Add files to database
#package:odbgmusic
#type:local

CONFIGFILE="/etc/opendomo/bgmusic.conf"
. $CONFIGFILE
TMPPLIST=/tmp/bgmusic.tmplist

# Clean album parameter

ALBUM=""

ROUTE=/media
IFS=$'\x0A'$'\x0D'


# if $1 is "add" the user has selected artist and album, if not the files from browseFiles.sh

if test "$1" == "add" ; then
	if ! test -z $3; then 
		
		# Change parameters (names with space)

		ARTIST=`echo "$2" | tr + " "` 	
		ALBUM=`echo "$3" | tr +  " "`
			
		# Add files to database and clear temporal file
	
		/usr/local/opendomo/bgmusicManageDB.sh add $ARTIST $ALBUM $COVER
		echo "#INFO mp3 files added to database"
			
		# Clean files and variables

		rm $TMPPLIST
		ARTIST=""
		ALBUM=""
		COVER=""
		
	fi
else

	for i in $@; do
		ADDTOLIST=`echo $ROUTE$i | tr + " "`

		# The parameter is a directory or file

		if test -d "$ADDTOLIST"; then		
			for dir in `ls -1R $ADDTOLIST | grep : | cut -f1 -d:`; do # List a directories
				for file in `ls -1 $dir | grep .mp3`; do
					echo "$dir/$file" >>$TMPPLIST # List mp3 files in recursive mode
				done
			done
		else
			EXT=`echo "$i" | cut -d. -f2`

			# The parameter is mp3 file
			if test "$EXT" = "mp3"; then	
				echo "$ADDTOLIST" >>$TMPPLIST
			fi
		fi
	done
fi

# Web interface

echo "#> Songs"
echo "list:`basename $0`"
for song in `cat $TMPPLIST`; do
	echo "	-$song	$song	song"
done
echo "actions:"
echo "	bgmusicCleanList.sh	Clean list"
echo "	browseFiles.sh	Select other files"
echo
echo "#> Options"
echo "form:`basename $0`"
echo "	ACTION	Add	hidden	add"
echo "	ARTIST	Artist	text	$ARTIST"
echo "	ALBUM	Album	text	$ALBUM"
echo "	COVER	Cover file	text	$COVER"
echo

